#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#include "pbd_error.h"

void pbd_print(int level, const char *fname, const char *fmt, ...)
{
	va_list ap;
	char logbuf[512];
	int printfname = 1;

	// not care
	if(level < PBD_LOG_LEVEL)
		return;

	va_start(ap, fmt);
	vsnprintf(logbuf, sizeof(logbuf), fmt, ap);

	if (*fname == '\0')
		printfname = 0;

	time_t now;
	struct tm *tm_now;

	if ((now = time(NULL)) < 0)
		return;
	tm_now = localtime(&now);

	fprintf(stderr, "%02d:%02d:%02d: %s%s%s",
		tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec,
		fname, printfname ? ": " : "", logbuf);
}

void *alloc_pbd_mem(int size)
{
	char *c = malloc(size);
	if(!c){
		pbd_print(PBD_LOG_ERR, PBD_FUN, "Allocate Memory Fail\n");
		return NULL;
	}
	memset(c, 0, size);

	return c;
}

void free_pbd_mem(void *p)
{
	free(p);
}

